import { defineStore } from 'pinia'
import { store } from '../index'
import { useStorage } from '@/hooks/web/useStorage'

const { getStorage, setStorage } = useStorage('localStorage')

interface OtherState {
  commonContentType: string | number
  commonContentData: any[]
  projectId: string
  orgId: string
  orgName: string
  projectInfo: any
}
export const useOtherStore = defineStore('other', {
  state: (): OtherState => {
    return {
      commonContentType: 0,
      commonContentData: [],
      projectId: '',
      orgId: '',
      orgName: '',
      projectInfo: getStorage('projectInfo')
    }
  },
  getters: {
    getContentType(): string | number {
      return this.commonContentType
    },
    getProjectId(): string {
      return this.projectId
    },
    getProjectInfo(): any {
      return this.projectInfo
    },
    getOrgId(): any {
      return this.orgId
    },
    getOrgName(): any {
      return this.orgName
    },
    getContentData(): any[] {
      return this.commonContentData
    }
  },
  actions: {
    setContentType(commonContentType: string | number) {
      this.commonContentType = commonContentType
    },
    setOrgInfo(info: { orgId: string; orgName: string }) {
      this.orgId = info.orgId
      this.orgName = info.orgName
    },
    setProjectId(projectId: string) {
      this.projectId = projectId
    },
    setProjectInfo(projectInfo: any) {
      this.projectInfo = projectInfo
      if (projectInfo.id) {
        useOtherStore().setProjectId(projectInfo.id)
      }
      setStorage('projectInfo', projectInfo)
    },
    setContentData(commonContentData: any[]) {
      this.commonContentData = commonContentData
    }
  },
  persist: true
})

export const useOtherStoreWithOut = () => {
  return useOtherStore(store)
}
